package com.itheima.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.*;
import com.itheima.po.Book;
import com.itheima.po.Comment;
import com.itheima.po.User;
import com.itheima.service.BookService;
import com.itheima.service.CommentService;
@Controller
public class BookController {
	@Autowired
	private CommentService commentService;
	@Autowired
	private BookService bookService;
	/*
	 * 获取图书所有信息，包括评论
	 */
	@RequestMapping("/getBookInfo.action")
	@ResponseBody
	public JSONObject getBookInfo(int bookid) {
		Book book = bookService.getBookInfo(bookid);
		List<Comment> comments = commentService.getBookComments(bookid);
		JSONObject json = (JSONObject) JSONObject.toJSON(book);
		json.put("comments", JSONArray.parseArray(JSON.toJSONString(comments)));
		return json;
	}
	/*
	 * 管理员上传图书
	 */
	@RequestMapping("/uploadBook.action")
	@ResponseBody
	public JSONObject uploadBook(Book book) {
		JSONObject json = new JSONObject();
		int rows = bookService.uploadBook(book);
		if(rows>0) {
			json.put("status", "succeed");
			json.put("bookid", book.getBookid());
		}else
			json.put("status", "failed");
		return json;
		
	}
	/*
	 * 上传图书封面
	 */
	@RequestMapping("/uploadBookcover.action")
	@ResponseBody
	public JSONObject uploadBookcover(@RequestParam MultipartFile uploadfile,HttpServletRequest request) {
		String fileName = uploadfile.getOriginalFilename();
//		String coverRoot = "booksCover/";
//		String dirPath = request.getServletContext().getRealPath(coverRoot);	
		String dirPath = "F:/booksCover";
		File file = new File(dirPath);
		
		if(!file.exists())
			file.mkdirs();
		String newFileName = UUID.randomUUID()+"_"+fileName;
		String cover_url=dirPath+"/"+newFileName;
		String cover = "booksCover/"+newFileName;
		System.out.println(cover_url);
		try {
			uploadfile.transferTo(new File(cover_url));
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JSONObject json = new JSONObject();
		json.put("cover", cover);
		return json;
		
	}
	/*
	 * 搜索图书
	 */
	@RequestMapping("/searchBook.action")
	@ResponseBody
	public JSONObject searchBook(String keyword) {
		JSONObject json=new JSONObject();
		JSONArray jarray = JSONArray.parseArray(JSON.toJSONString(bookService.searchBook(keyword)));
		json.put("books", jarray);
		return json;
		
	}
	/*
	 * 更新评分
	 */
	@RequestMapping("/updateStar.action")
	@ResponseBody
	public void updateStar() {
		List<Integer> idList = bookService.getIdList();
		for(Integer bookid:idList) {
			List<Integer> list = commentService.getStarList(bookid);
			list.sort(null);
			int size = list.size();
			if(size<=5) {
				bookService.updateStar(bookid,0);//评分人数小于5人默认0分
			}else {
				double star=0;
				list = list.subList(size/5, size-size/5);//去掉五分之一最高分和最低分
				for(Integer i : list) {
					star+=i;
				}
				star = (double)star/list.size();
				bookService.updateStar(bookid,star);
			}
		}

	}
	/*
	 * 获取某一类型图书总数,方便分页
	 */
	@RequestMapping("/getBookNumOfType.action")
	@ResponseBody
	public JSONObject getBookNumOfType(String type) {
		JSONObject json = new JSONObject();
		json.put("num", bookService.getBookNumOfType(type));
		return json;
	}
	/*
	 * 获取某一类型的13本图书
	 */
	@RequestMapping("/getBooks.action")
	@ResponseBody
	public JSONObject getBooks(int page,String type) {
		JSONObject json = new JSONObject();
		List<Book> books = bookService.getBooks(13*(page-1), type);
		JSONArray jarray = JSONArray.parseArray(JSON.toJSONString(books));
		json.put("books", jarray);
		return json;
	}
}
